#include"ring_queue.hpp"
#include"Task.hpp"
#include<stdlib.h>

using namespace ring_queue;
using namespace std;


void* consumer(void* args)
{
    RingQueue<Task<int>>* rq = (RingQueue<Task<int>>*)args;
    while(true)
    {
        sleep(1);
        Task<int> t;
        rq->pop(&t);
        t.Run();
    }
}
void* producter(void* args)
{
    RingQueue<Task<int>>* rq = (RingQueue<Task<int>>*)args;
    const char* table = "+-*/%";
    while(true)
    {
        sleep(1);
        
        int x = rand()%20+1;
        int y = rand()%10+1;
        char ch = table[rand()%5];
        Task<int> t(x, y, ch);
        rq->push(t);
        //cout<<"我是生产["<< pthread_self() <<"]，我生产了一个任务：" << x << ch << y << "=？" << endl;

    }
}

int main()
{
    srand((unsigned int)time(nullptr));
    RingQueue<Task<int>>* rq = new RingQueue<Task<int>>();
    pthread_t c0;pthread_t c1;pthread_t c2, c3;
    pthread_t p0;pthread_t p1;pthread_t p2;
    pthread_create(&c0, nullptr, consumer, (void*)rq);
    pthread_create(&c1, nullptr, consumer, (void*)rq);
    pthread_create(&c2, nullptr, consumer, (void*)rq);
    pthread_create(&c3, nullptr, consumer, (void*)rq);

    pthread_create(&p0, nullptr, producter, (void*)rq);
    pthread_create(&p1, nullptr, producter, (void*)rq);
    pthread_create(&p2, nullptr, producter, (void*)rq);


    pthread_join(c0,nullptr);
    pthread_join(c1,nullptr);
    pthread_join(c2,nullptr);
    pthread_join(c3,nullptr);
    pthread_join(p0,nullptr);
    pthread_join(p1,nullptr);
    pthread_join(p2,nullptr);
}